package com.hyx.bookManager.server;

import com.hyx.bookManager.mapper.MonitorMapper;
import com.hyx.bookManager.utils.EmailUtil;
import com.hyx.bookManager.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.websocket.Session;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @ClassName ImageServer
 * @Description TODO
 * @Author 黄乙轩
 * @Date 2021/6/28 11:10
 * @Version 1.0
 **/

public class ImageServer implements Runnable {

    private MonitorMapper monitorMapper;
    private EmailUtil emailUtil;

    private static final long serialVersionUID = 2839564863495205814L;

    ServerSocket ss;

    public ImageServer(MonitorMapper monitorMapper, EmailUtil emailUtil) throws Exception {
        //监听端口
        this.monitorMapper=monitorMapper;
        this.emailUtil = emailUtil;
        System.out.println(monitorMapper);
        ss = new ServerSocket(9000);
        new Thread(this).start();
    }

    @Override
    public void run() {
        int i = 0;
        System.out.println("server startup.");
        while (true) {
            try {
                Socket s = ss.accept();
                // 每个客户端一个处理线程
                new Handler(s, i,monitorMapper,emailUtil).start();
                i++;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


}

class Handler extends Thread {
    Socket s;
    int id;
    private MonitorMapper monitorMapper;
    private EmailUtil emailUtil;

    public Handler(Socket s, int id, MonitorMapper monitorMapper, EmailUtil emailUtil) {
        this.s = s;
        this.id = id;
        this.monitorMapper=monitorMapper;
        this.emailUtil = emailUtil;
    }

    @Override
    public void run() {
        System.out.println("in handling..");

        FileOutputStream fos = null;
        InputStream is = null;
        try {
            is = s.getInputStream();
            BufferedReader in = new BufferedReader(new InputStreamReader(is));


            String filename = System.currentTimeMillis() + ".jpg";

            String filepath = "F:\\项目\\monitorImg\\";

            File dirFile = new File(filepath);
            if (!dirFile.exists()) {
                dirFile.mkdirs();
            }

            System.out.println("read line " + id + " :" + filename);

            LocalDateTime time = LocalDateTime.now();
            monitorMapper.insertMonitorImg(id,filename,time);


            File file = new File(filepath + filename);

            int len = 0;
            int BUFSIZE = 1 * 1024;

            byte[] bytes = new byte[BUFSIZE];

            fos = new FileOutputStream(file);
            while ((len = is.read(bytes, 0, bytes.length)) != -1) {
                fos.write(bytes, 0, len);
                fos.flush();
            }



            System.out.println("done.");


            FileInputStream fs = new FileInputStream(filepath + filename);

            emailUtil.sendMonitorEmail("843497509@qq.com",filename);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 服务端就不要手贱 关了socket否则Python 会出现错误Errno 10054让客户端关掉就行啦
            try {
                System.out.println("close");
                fos.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
